﻿using Hang.Blog.Entities;
using Hang.Blog.Users;

using Microsoft.EntityFrameworkCore;

using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Identity;
using Volo.Abp.Users.EntityFrameworkCore;

namespace Hang.Blog.EntityFrameworkCore {
    /* This is your actual DbContext used on runtime.
     * It includes only your entities.
     * It does not include entities of the used modules, because each module has already
     * its own DbContext class. If you want to share some database tables with the used modules,
     * just create a structure like done for AppUser.
     *
     * Don't use this DbContext for database migrations since it does not contain tables of the
     * used modules (as explained above). See BlogMigrationsDbContext for migrations.
     */
    [ConnectionStringName("Default")]
    public class BlogDbContext : AbpDbContext<BlogDbContext> {
        public DbSet<AppUser> Users { get; set; }


        public DbSet<AppRole> Roles { get; set; }



        public DbSet<Menu> Categories { get; set; }


        public DbSet<Article> Articles { get; set; }

        public DbSet<ArticleTag> ArticleTags { get; set; }  
        
     
        public  DbSet<ArticleAndTag> ArticleAndTags { get; set; }

        public DbSet<Category> Category { get; set; }

        /* Add DbSet properties for your Aggregate Roots / Entities here.
         * Also map them inside BlogDbContextModelCreatingExtensions.ConfigureBlog
         */

        public BlogDbContext(DbContextOptions<BlogDbContext> options)
            : base(options) {

        }

        protected override void OnModelCreating(ModelBuilder builder) {
            base.OnModelCreating(builder);

            /* Configure the shared tables (with included modules) here */

            builder.Entity<AppUser>(b => {
                b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser

                b.ConfigureByConvention();
                b.ConfigureAbpUser();

                /* Configure mappings for your additional properties
                 * Also see the BlogEfCoreEntityExtensionMappings class
                 */
            });

            builder.Entity<AppRole>(d => {
                d.ToTable("AbpRoles");
                d.ConfigureByConvention();
                d.Property(x => x.Title).HasMaxLength(128);
            });

        

            /* Configure your own tables/entities inside the ConfigureBlog method */

            builder.ConfigureBlog();
        }
    }
}
